package nz.ac.massey.groupproject.ecs2;

import java.util.ArrayList;


public final class ECSEntity {
	// ArrayMap
	private final ArrayList<ECSComponent>    comps  =  new ArrayList<ECSComponent>();

	public ECSEntity(ECSComponent... comps_) {
		for (var c : comps_)
			comps.add(c);
	}

	public boolean hasComponent(Class type) { return getComponent(type) != null; }
	public boolean has(Class type) { return hasComponent(type); }

	// Normally you don't need to call this.
	//
	// If you want to access some optional components, you can use System method signature
	// to match required components, then call getComponent() to retrieve optional components.
	public ECSComponent getComponent(Class type) {
		assert ECSComponent.class.isAssignableFrom(type) : "entity.getComponent(cls) called with non-Component class";

		for (var comp : comps)
			if (type.isInstance(comp))
				return comp;
		return null;
	}
	public ECSComponent get(Class type) { return getComponent(type); }

	// Add if entity doesn't have this component.  Replace if it does.
	//
	// XXX component type must match exactly. Does not work with inheritance.
	public void setComponent(ECSComponent comp) {
		for (int i = 0; i < comps.size(); i++)
			if (comp.getClass() == comps.get(i).getClass()) {
				comps.set(i, comp);
				return;
			}
		comps.add(comp);
	}
	public void set(ECSComponent comp) { setComponent(comp); }
}
